﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VSDBM.Code.dto;
using System.Data;
using MySql.Data.MySqlClient;

namespace VSDBM.Code.dao
{
    public class UserConfigDAO
    {
        public static String add(UserConfig config)
        {
            try
            {
                MySqlConnection connection = Connection.getConnection();

                
                
                    MySqlCommand query = new MySqlCommand("INSERT INTO user_config (userid) VALUES (" + config.userid + ")", connection);
                    query.ExecuteNonQuery();
                    return "Added!";
                
                
            }
            catch (Exception e)
            {
                return "ERRO! - " + e.Message;
            }
        }

        public static Boolean update(UserConfig config)
        {
            try
            {
                MySqlConnection connection = Connection.getConnection();
                MySqlCommand query = new MySqlCommand("UPDATE user_config SET userid="+config.userid+",blast_wordsize="+config.blast_wordsize+",blast_mindiagonallength="+config.blast_mindiagonallength+",blast_teshold="+config.blast_teshold+",blast_hitscore="+config.blast_hitscore +",blast_misspenality="+config.blast_misspenality+",blast_gapexistpenality="+config.blast_gapexistpenality+",blast_gapextendpenality="+config.blast_gapextendpenality+",blast_unambiguityscoreteshold="+ config.blast_unambiguityscoreteshold+" WHERE userid ="+config.userid, connection);
                query.ExecuteNonQuery();
                
            }
            catch (Exception)
            {
                
                throw;
                
            }
            return true;
        }

        public static Boolean delete(int userId)
        {
            try
            {
                MySqlConnection connection = Connection.getConnection();
                MySqlCommand query = new MySqlCommand("DELETE ON user_config WHERE userid = " + userId + "", connection);
                query.ExecuteNonQuery();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

       

        public static DataSet select(String filters, String orderby)
        {

            DataSet dSet = new DataSet();
            try
            {
                MySqlConnection connection = Connection.getConnection();
                MySqlDataAdapter adapter = new MySqlDataAdapter();
                adapter.SelectCommand = new MySqlCommand("SELECT * FROM user_config " + filters + " " + orderby, connection);
                adapter.Fill(dSet);
                return dSet;
            }
            catch (Exception e)
            {
                return dSet;
            }
        }

        public static UserConfig getConfig(int userid)
        {
            UserConfig config = new UserConfig();
            config.userid = userid;
            DataSet ds = UserConfigDAO.select("WHERE userid=" + userid, "");
            if (ds.Tables[0].Rows.Count > 0)
            {
                config.id = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
                config.userid = Convert.ToInt32(ds.Tables[0].Rows[0][1]);
                config.blast_wordsize = Convert.ToInt32(ds.Tables[0].Rows[0][2]);
                config.blast_mindiagonallength = Convert.ToInt32(ds.Tables[0].Rows[0][3]);
                config.blast_teshold = Convert.ToInt32(ds.Tables[0].Rows[0][4]);
                config.blast_hitscore = Convert.ToSingle(ds.Tables[0].Rows[0][5]);
                config.blast_misspenality = Convert.ToSingle(ds.Tables[0].Rows[0][6]);
                config.blast_gapexistpenality = Convert.ToSingle(ds.Tables[0].Rows[0][7]);
                config.blast_gapextendpenality = Convert.ToSingle(ds.Tables[0].Rows[0][8]);
                config.blast_unambiguityscoreteshold = Convert.ToSingle(ds.Tables[0].Rows[0][9]);
            }
            else
            {
                UserConfigDAO.add(config);
                return getConfig(userid);
            }
            return config;
        }


    }
}